package com.miniboy.imageloader.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 作者 ${郭鹏飞}.<br/>
 */
public class NetUtil {

    private NetUtil() {
    }

    /**
     * 判断当前网络是否可用
     *
     * @param context Context
     * @return <ul><li>true 当前网络可用</li><li>false 当前网络不可用</li></ul>
     */
    public static boolean isNetWorkCanUse(Context context) {
        if (context == null) {
            throw new NullPointerException("context 为空");
        }
        ConnectivityManager manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        return info != null && info.isConnected();
    }

    /**
     * 获取网络类型
     *
     * @param context Context
     * @return -1表示没有网络 其他具体数据请查看{@link ConnectivityManager}
     */
    public int getNetworkType(Context context) {
        if (context == null) {
            throw new NullPointerException("context 为空");
        }
        ConnectivityManager manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if (info == null) {
            return -1;
        }
        return info.getType();
    }
}
